package njust.edu.dao.impl;

import njust.edu.dao.RoleDao;
import njust.edu.dbutils.SimpleUtils;
import njust.edu.entity.DishType;
import njust.edu.entity.Role;
import njust.edu.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl  implements RoleDao {

    @Override
    public Role findById(Integer id) {
        String sql = "select id,name from roles where id = ?";
        Role role = new Role();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            while(rs.next()){
                role  = new Role(rs.getInt(1),
                        rs.getString(2)
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return role;
    }

    @Override
    public List<Role> getList() {
        String sql = "SELECT id,name " + "FROM roles";
        List<Role> R = new ArrayList<>();
        Role role = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                role  = new Role(rs.getInt(1),rs.getString(2));
                R.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return R;
    }

}
